from django.test import TestCase
from myhomework.models import User,Group
from myhomework.account.daos import *
from myhomework.exceptions.exception import *
from django.contrib.auth.models import User as AuthUser
from django.contrib.auth.models import Permission

class GroupViewTest(TestCase):
    def setUp(self):
        self.user1 = User(name='user1',student_id='08386041',email='1234@163.com',major='CAS')
        UserDao.add_user(self.user1)
        self.user2 = User(name='user2',student_id='08386042',email='56789@163.com',major='CAS')
        UserDao.add_user(self.user2)
        self.user3 = User(name='user3',student_id='08386043',email='78901@163.com',major='CAS')
        UserDao.add_user(self.user3)
        
        self.user4 = User(name='user4',student_id='08386044',email='1234@163.com',major='CAS')
        UserDao.add_user(self.user4)
        self.user5 = User(name='user5',student_id='08386045',email='56789@163.com',major='CAS')
        UserDao.add_user(self.user5)
        self.user6 = User(name='user6',student_id='08386046',email='78901@163.com',major='CAS')
        UserDao.add_user(self.user6)
        
        self.user7 = User(name='user7',student_id='08386047',email='1234@163.com',major='CAS')
        UserDao.add_user(self.user7)
        self.user8 = User(name='user8',student_id='08386048',email='56789@163.com',major='CAS')
        UserDao.add_user(self.user8)
        self.user9 = User(name='user9',student_id='08386049',email='78901@163.com',major='CAS')
        UserDao.add_user(self.user9)
        
        
        self.group1 = Group(name='group1',administrator = self.user1)
        self.group1.save()
        self.user1.group=self.group1
        self.user1.save()
        PermissionDao.user_add_perms(self.user1.auth_user,['group1.admin','group1.member'])
    
        
        self.group2 = Group(name='group2',administrator = self.user2)
        self.group2.save()
        self.user2.group=self.group2
        self.user2.save()
        PermissionDao.user_add_perms(self.user2.auth_user,['group2.admin','group2.member'])
        
        
        self.group3 = Group(name='group3',administrator = self.user3)
        self.group3.save()
        self.user3.group=self.group3
        self.user3.save()
        PermissionDao.user_add_perms(self.user3.auth_user,['group3.admin','group3.member'])
        
        
        self.user4.group=self.group1
        self.user4.save()
        self.group1.save()
        PermissionDao.user_add_perms(self.user4.auth_user,['group1.member'])
        
        
        self.user5.group=self.group2
        self.user5.save()
        self.group2.save()
        PermissionDao.user_add_perms(self.user5.auth_user,['group2.member'])
        
        
        
    def tearDown(self):
        for user in User.objects.all():
            user.auth_user.delete()
            user.delete()
            
        for group in Group.objects.all():
            group.delete()
        for perm in Permission.objects.all():
            perm.delete()
            
    def test_user_join_group(self):
        
        #join successfully
        self.client.login(username='08386046', password='08386046')
        response = self.client.get('/user_join_group',{'group':'group1'})
        self.assertEquals(response.status_code, 200)
        self.assertEquals(GroupDao.is_user_join_this_group(group_name='group1',user_id='08386046'),True)
        self.client.logout()
        
        self.client.login(username='08386045', password='08386045')
        
        #user still in another group
        self.assertRaises(UserStillInAGroup,self.client.get,'/user_join_group',{'group':'group1'})
        
        #group not existed
        self.assertRaises(GroupNotExisted,self.client.get,'/user_join_group',{'group':'group10'})
        
        self.client.logout()

    def test_user_quit_group(self):
        
        #user not in any group
        self.client.login(username='08386049', password='08386049')  
        self.assertRaises(UserNotInAnyGroup,self.client.get,'/user_quit_group',{'group':'group1'})
        self.client.logout()
        
        #user in another group
        self.client.login(username='08386044', password='08386044')  
        self.assertRaises(UserNotInThisGroup,self.client.get,'/user_quit_group',{'group':'group3'})
        self.client.logout()
        
        #UserIsAministrator
        self.client.login(username='08386041', password='08386041')  
        self.assertRaises(UserIsAministrator,self.client.get,'/user_quit_group',{'group':'group1'})
        self.assertRaises(GroupNotExisted,self.client.get,'/user_join_group',{'group':'group10'})
        self.client.logout()
       
        #a member quit group
        self.client.login(username='08386044', password='08386044')  
        response = self.client.get('/user_quit_group',{'group':'group1'})
        self.assertEquals(response.status_code, 200)
        self.assertEquals(GroupDao.is_user_join_this_group(group_name='group1',user_id='08386044'),False)
        self.assertEquals(PermissionDao.user_has_perms(self.user4.auth_user,['group1.admin','group1.member']),False)
        self.client.logout()
        
        #a administrator quit group, group has only a administrator
        self.client.login(username='08386043', password='08386043')  
        response = self.client.get('/user_quit_group',{'group':'group3'})
        self.assertEquals(response.status_code, 200)
        self.assertEquals(PermissionDao.user_has_perms(self.user3.auth_user,['group3.admin','group3.member']),False)
        self.assertRaises(GroupNotExisted,GroupDao.get_group_by_name,'group3')
     
        self.client.logout()
    
    def test_delete_group(self):
        
        #group not existed
        self.client.login(username='08386041', password='08386041')  
        self.assertRaises(GroupNotExisted,self.client.get,'/user_join_group',{'group':'group10'})
        self.client.logout()
        
        #delete successfully, group has 2 or more member
        self.client.login(username='08386041', password='08386041')  
        
        response = self.client.get('/delete_group',{'group':'group1'})
        self.assertEquals(PermissionDao.user_has_perms(self.user1.auth_user,['group1.admin','group1.member']),False)
        self.assertEquals(PermissionDao.user_has_perms(self.user4.auth_user,['group1.admin','group1.member']),False)
        self.assertEquals(UserDao.is_user_in_a_group(self.user1.student_id),False)
        self.assertEquals(UserDao.is_user_in_a_group(self.user4.student_id),False)
        self.assertRaises(GroupNotExisted,GroupDao.get_group_by_name,'group1')
        
        self.client.logout()
        
        #delete group has only aadministrator
        self.client.login(username='08386041', password='08386041')  
        
        response = self.client.get('/delete_group',{'group':'group3'})
        self.assertEquals(PermissionDao.user_has_perms(self.user3.auth_user,['group3.admin','group3.member']),False)
        self.assertEquals(UserDao.is_user_in_a_group(self.user3.student_id),False)
        self.assertRaises(GroupNotExisted,GroupDao.get_group_by_name,'group3')
        
        self.client.logout()
      
    def admin_quit_user_from_group(self):
        pass
        